package com.mcy.springbatch.reader;

import com.mcy.springbatch.entity.SysUser;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.database.JpaPagingItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.persistence.EntityManagerFactory;
import java.util.HashMap;
import java.util.Map;

/**
 * 利用JpaPagingItemReader从数据库读取数据
 */
@StepScope
@Component
public class ReadUserDataJpaPagingItemReader extends JpaPagingItemReader<SysUser> {

    @Autowired
    EntityManagerFactory entityManagerFactory;

    /**
     * 不能在构造函数注入EntityManagerFactory
     */
    @PostConstruct
    public void init(){
        setEntityManagerFactory(entityManagerFactory);
        setPageSize(20);
        setQueryString("select new SysUser(id,loginName,  userName,  idCard,  phone,  sex,  address,  email,  status) from SysUser where id > :id");
        Map<String, Object> params = new HashMap<>();
        try{
            params.put("id", 1);
        }catch (Exception e){

        }
        setParameterValues(params);
        setSaveState(false);
    }
}
